﻿using Learun.Application.Organization;
using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.Message
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2019-09-10 10:12
    /// 描 述：消息记录管理
    /// </summary>
    public class MSG_RecordService
    {
        #region 仓储
        Repository<MES_MSG_RecordEntity> _recordRepository => new Repository<MES_MSG_RecordEntity>();
        UserService userService = new UserService();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<MES_MSG_RecordEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"
                t.*
                ");
                strSql.Append("  FROM MES_MSG_Record t ");
                strSql.Append("  WHERE 1=1 ");
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty())
                {
                    dp.Add(new SugarParameter("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime));
                    dp.Add(new SugarParameter("endTime", queryParam["EndTime"].ToDate(), DbType.DateTime));
                    strSql.Append(" AND ( t.CreateTime >= @startTime AND t.CreateTime <= @endTime ) ");
                }
                if (!queryParam["Title"].IsEmpty())
                {
                    dp.Add(new SugarParameter("Title", "%" + queryParam["Title"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.Title Like @Title ");
                }
                if (!queryParam["Receiver"].IsEmpty())
                {
                    dp.Add(new SugarParameter("Receiver",queryParam["Receiver"].ToString(), DbType.String));
                    strSql.Append(" AND t.Receiver = @Receiver ");
                }
                if (!queryParam["MsgType"].IsEmpty())
                {
                    dp.Add(new SugarParameter("MsgType",queryParam["MsgType"].ToString(), DbType.String));
                    strSql.Append(" AND t.MsgType = @MsgType ");
                }
                //return this.BaseRepository().FindList<MES_MSG_RecordEntity>(strSql.ToString(),dp, pagination);
                return SqlSugarHelper.Db.SqlQueryable<MES_MSG_RecordEntity>(strSql.ToString()).AddParameters(dp).ToPageList(pagination.page, pagination.rows);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取MES_MSG_Record表实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public MES_MSG_RecordEntity GetMES_MSG_RecordEntity(string keyValue)
        {
            try
            {
                return _recordRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                _recordRepository.Delete(t=>t.ID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, MES_MSG_RecordEntity entity)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    _recordRepository.Update(entity);
                }
                else
                {
                    entity.Create();
                    _recordRepository.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
